from entity import Solution
from data import Data
from math import erf


def __restrict3(sol: Solution):
    value = sol.As1 / (400 * (1300 - sol.as1))
    switch = {
        14.3: 0.002 <= value <= 0.0206,
        16.7: 0.002 <= value <= 0.024,
        19.1: 0.00214 <= value <= 0.0274,
        21.1: 0.00225 <= value <= 0.0305,
    }
    return switch[sol.concrete.fc]


class Calculate:
    # 约束条件里列表
    __restrict_list = [
        lambda sol: 153.52 * sol.concrete.fc * pow((1300 - sol.as1), 2) +
                    360 * sol.As2 * (1300 - sol.as1 - sol.as2) >= 2.6 * pow(10, 9),
        lambda sol: 153.52 * sol.concrete.fc * pow((1300 - sol.as1), 2) +
                    (360 * sol.As1 - 207.2 * sol.concrete.fc * (1300 - sol.as1)) * (1300 - sol.as1 - sol.as2)
                    >= 2.6 * pow(10, 9),
        lambda sol: __restrict3(sol),
        lambda sol: 65 <= sol.as1 <= 100 and 35 <= sol.as2 <= 100,
        lambda sol: 0.01 - 0.01 * erf(sol.as1 / (pow(pow(10, -11.27) * 50, 0.5))) <= 0.005
                    and 0.01 - 0.01 * erf(sol.as2 / (pow(pow(10, -11.27) * 50, 0.5))) <= 0.005,
        lambda sol: 1.9 * (1.1 - (147000 * (1300 - sol.as1) * sol.concrete.ftk) / 2000000000)
                    * (2000000000 / (174000 * (1300 - sol.as1) * sol.As1)) * (1.9 * sol.as1 + 520000 / sol.As1) <= 0.3
    ]

    @staticmethod
    def cost_price(sol: Solution, b=400, h=1300, c1=5000, y1=0.000078):
        return sol.concrete.c0 * (b * h - sol.As1 - sol.As2) + c1 * y1 * (sol.As1 + sol.As2)

    @staticmethod
    def check(sol: Solution):
        for restrict in Calculate.__restrict_list:
            if not restrict(sol):
                return False
        return True

    # TODO 如何随机生成一个可行解？ 或者随便什么解都可以？
    @staticmethod
    def init_solution():
        return Solution(1, 2, 3, 4, Data().get_concrete("C30"))
